var JoinRoomMy = function() {
	thisJoinRoomMy = this;

	this.TAG = " < JoinRoomMy > ";
	this.cm = null;
	this.mListData = null;
	this.pageControl = null;
	this.room_add_btn_form = null;
	this.userId = null;
//    reservation_sid_name = ["RS001", "WR001", "DR001"];
//    reservation_service_name = ["reservSearchList", "WatingreservMainSearchList", "DonationMainReservList"];
//    search_reservation = 0;
//    search_area = 0;
//    search_course = 0;
	this.listCount = 10;
};

JoinRoomMy.prototype = {
    constructor : JoinRoomMy,
    getJoinRoomData : function(reservation, area, step)
    {
    	console.log(this.TAG + "getJoinRoomData()");
        var mJson = new MJson();

//        mJson.putDevice(MJson.DEVICE.OID, "0001"); // 기본설정
//        mJson.putDevice(MJson.DEVICE.SCREEN, "100");
//        mJson.putDevice(MJson.DEVICE.DVID, "100");
//        mJson.putDevice(MJson.DEVICE.MODEL, "Iphone4");
//        mJson.putDevice(MJson.DEVICE.FIRMWARE, "ios5");

        mJson.putClient(MJson.CLIENT.USERID, this.userId );
        
        mJson.putPub(MJson.PUB.SID, "JR001");
        mJson.putPub(MJson.PUB.SERVICENAME, "getMyJoinRoomList");
        mJson.putPub(MJson.PUB.LISTCOUNT, this.listCount);
        mJson.putPub(MJson.PUB.CURRPAGENO, thisJoinRoomMy.pageControl.currentPage);
        
        mJson.putParameter(MJson.PARAMETER.BOOKINGTIME, "");
        //mJson.putParameter(MJson.PARAMETER.USERID, this.userId);
        //alert(document.globalObj.userId);
        mJson.putParameter(MJson.PARAMETER.NICKNAME, "");
        mJson.putParameter(MJson.PARAMETER.LOCATION, "");
        mJson.putParameter(MJson.PARAMETER.RESERVEST, "");
        mJson.putParameter(MJson.PARAMETER.RESERVEET, "");
                
        thisJoinRoomMy.cm.requestGet(mJson.getJsonString(), function(jsonData) {
        	
        	if(jsonData.msp.contents) 
        	{

	        	if(jsonData.msp.contents.items != undefined) {
		        	if(jsonData.msp.contents.items.length != undefined)
		        	{
			        	for(var i=0; i<jsonData.msp.contents.items.length; i++) 
			        	{
			        		thisJoinRoomMy.mListData.push(jsonData.msp.contents.items[i]);
			        	}
		        	}
		        	else {
		        		thisJoinRoomMy.mListData.push(jsonData.msp.contents.items);
		        	}
		
		        	
	        	}
        	}
        	
        	thisJoinRoomMy.viewList(jsonData);
        	
        }, function(e) {
        	alert("error :: " + JSON.stringify(e));
        });
    },
    onPageShow : function() {
    	console.log(this.TAG + "onPageShow()");
    	this.userId = document.globalObj.LOGIN_DATA.userId;
    	
    	// 변수 초기화
    	thisJoinRoomMy.cm = new ConnectionManager();
    	thisJoinRoomMy.mListData = new Array();
    	thisJoinRoomMy.pageControl = new PageControl();
    	thisJoinRoomMy.room_add_btn_form = $("#add_btn");

    	thisJoinRoomMy.pageControl.clear();
    	
    	thisJoinRoomMy.initEvent();

    	thisJoinRoomMy.mListData = null;
    	thisJoinRoomMy.mListData = new Array();
    	
    	// 초기 부킹 데이터 가져오기
    	thisJoinRoomMy.getJoinRoomData(null, null, null);
    	
    	// 버튼 이벤트 등록
    },
    viewList: function(jsonData) 
    {
    	console.log(this.TAG + "viewList()");
    	
    	var listview = $("#my_listview");
    	listview.empty();
    	//listview.children().remove('li');
    	
    	for(var i=0; i<thisJoinRoomMy.mListData.length; i++)
    	{
    		var nickname = null; // 대화명
    		
    		if(thisJoinRoomMy.mListData[i].nickName != undefined)
    			nickname = "<li class=nickname>작성자 :<mark>" +thisJoinRoomMy. mListData[i].nickName + "</mark></li>";
    		else
    			nickname = "<li class=nickname>작성자 :<mark>익명</mark></li>";
    		
    		var reservDate = null; // 부킹 예약날짜
    		
    		if(thisJoinRoomMy.mListData[i].reserveDate != undefined) 
    			reservDate = "<li class=date>" + thisJoinRoomMy.mListData[i].reserveDate + "(" + thisJoinRoomMy.mListData[i].bookingDay + ")</li>";
    		else
    			reservDate = "";
    		
    		var procStatusNm = null; // 상태( 진행중, 마감, 조인완료 )
    		var procStatus = null; // 상태 C, D, E
    		if(thisJoinRoomMy.mListData[i].procStatusNm != undefined)
    		{
    			var spanClassName = null;
    			
    			if( thisJoinRoomMy.mListData[i].procStatus != undefined ) {
    				procStatus = thisJoinRoomMy.mListData[i].procStatus;
    				if(procStatus.toLowerCase() == "c")
    					spanClassName = "state_ing";
    				else if(procStatus.toLowerCase() == "d")
    					spanClassName = "state_finish";
    				else
    					spanClassName = "state_ok";
    			}
    			
    			procStatusNm = "<li class=state><span class="+spanClassName+">"+thisJoinRoomMy.mListData[i].procStatusNm+"</span></li></ul></a></li>";
    		}
    		else
    			procStatusNm = "";
    		
    		var replycnt = null;
    		if(thisJoinRoomMy.mListData[i].replycnt != undefined)
    			replycnt = "<li class=reply>댓글<mark>("+thisJoinRoomMy.mListData[i].replycnt+")</mark></li>";
    		else
    			replycnt = "<li class=reply>댓글<mark>(" + 0 + ")</mark></li>";
    		
    		var openYnForm = thisJoinRoomMy.mListData[i].openYn == "Y" ? "":"<li class=key><span class=ico_all ico1><em>비밀글</em></span></li>";
    		
    		var golfClubInfo = null;
    		
    		if(thisJoinRoomMy.mListData[i].golfClubInfo != undefined)
    			golfClubInfo = "<li class=golf_c>" + thisJoinRoomMy.mListData[i].golfClubInfo + "</li>";
    		else
    			golfClubInfo = "";
    		
    		var a_href = "#";
    		
    		var ment = "<li class='sub'>"+ thisJoinRoomMy.mListData[i].ment +"</li>" 
    		var ui = "<li id="+i+"><a href=" +a_href+ " data-transition=slide><ul>" +
    		ment +
    		reservDate +
			golfClubInfo + //"<li class=golf_c>스카이골프 오션코스오션코스오션코스오션코스</li>" +
			openYnForm +
			nickname +
			replycnt +
			procStatusNm; //"<li class=state><span class=state_finish>마감</span></li></ul></a></li>";
    		
    		var test_ui = "<li><a href=# data-rel=dialog data-transition=slide>비공개 조인룸 비밀번호 입력할때<ul><li class=date>2012.03.13(일)</li><li class=golf_c>스카이골프 오션코스오션코스오션코스오션코스</li><li class=key><span class=ico_all ico1><em>비밀글</em></span></li><li class=nickname>작성자 :<mark>잭니클라우스</mark></li><li class=reply>댓글<mark>(0)</mark></li><li class=state><span class=state_finish>마감</span></li></ul></a></li>";
    		listview.append(ui);
    		
    	}
    	
    	thisJoinRoomMy.room_add_btn_form.show();
    	if(jsonData.msp.contents) 
    	{
	    	if(thisJoinRoomMy.mListData.length >= jsonData.msp.contents["@totalcount"]) {
	    		//thisJoinRoom.pageNo.addView();
	    		thisJoinRoomMy.room_add_btn_form.hide();
	    	}
	    	else
	    		thisJoinRoomMy.room_add_btn_form.show();
    	}
    	
    	listview.listview("refresh");
    	
    	 //$("#total_listview > li").on("click", this.onClick );

    },
    initEvent: function() {
    	console.log(this.TAG + "initEvent()");
    	thisJoinRoomMy.room_add_btn_form.on("click", function(event) {
	    		thisJoinRoomMy.pageControl.addView();
	    		thisJoinRoomMy.getJoinRoomData(null, null, null);
    	});
    	
    	/**
    	 * joinRoomSeq를 join_room_view로 넘겨 서버연동하기 위함.
    	 */
    	$('#my_listview').delegate('li', 'click', function (event) {
    		console.log("리스트 아이템 클릭!!!");
    		
    		var positionId = $(this).attr("id");

    		var data = thisJoinRoomMy.mListData[positionId];
    		
    		if(data != undefined) {

    			if(data.joinRoomSeq != undefined) {
    	    		var joinRoomSeq = data.joinRoomSeq;
    	    		var password = data.password;
    				document.globalObj.joinRoomSeq = joinRoomSeq;
    				document.globalObj.joinRoomItemPw = password;
    			}
    			
    			document.globalObj.join_room_pw.afterChangeUrl = "join_room_view_my.html";
    			var changeUrl = data.openYn === "Y" ? "join_room_view_my.html" : "join_room_pw.html";
        		$.mobile.changePage(changeUrl);
    		}
    	});
    	
    	$("#my_search_join_room_btn").on("click", function($event){
    		$.mobile.changePage("join_room_search.html");
    	});
    	$("#my_total_join_room_btn").on("click", function($event){
    		$.mobile.changePage("join_room.html");
    	});
    	$("#my_create_join_room_btn").on("click", function($event){
    		$.mobile.changePage("join_room_creat.html");
    	});
    }  
};
